<template>
  <div>
    <div>
      <div class="shuju">
        <span class="el-icon-tickets"></span>
        <span> 数据列表</span>
        <span class="right">
          <el-button type="primary" size="small" @click="addfn()"
            >添加</el-button
          ></span
        >
        <span class="clear"></span>
      </div>
    </div>
    <div class="wei">
      <Suptable
        :tableData="tableData"
        :headerData="headerData"
        :isPage="false"
      >
        <template v-slot:opera="{ row }">
          <div>
            <el-button
              type="text"
              @click="goEdit('2', row.row.id, row.row)"
              size="small"
              >编辑</el-button
            >
            <el-button type="text" @click="godelete(row.row.id)" size="small"
              >删除</el-button
            >
          </div>
        </template>
      </Suptable>
    </div>
    <div>
      <el-dialog
        :title="dialogtype == '1' ? '新增' : '编辑'"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose"
      >
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="活动标题" prop="name" >
            <el-input v-model="ruleForm.name"></el-input>
          </el-form-item>
          <el-form-item label="原价" v-if="dialogtype==1">
            <el-input v-model="ruleForm.price1" ></el-input>
          </el-form-item>
          <el-form-item label="秒杀价">
            <el-input v-model="ruleForm.price2"></el-input>
          </el-form-item>
          <el-form-item label="秒杀数量">
            <el-input v-model="ruleForm.sesNum"></el-input>
          </el-form-item>
          <el-form-item label="剩余数量" v-if="dialogtype==1">
            <el-input v-model="ruleForm.remainNum" ></el-input>
          </el-form-item>
          <el-form-item label="限购数量">
            <el-input v-model="ruleForm.num"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >立即创建</el-button
            >
            <el-button @click="resetForm('ruleForm')" v-if="dialogtype==1">重置</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import Suptable from "@/components/Suptable";
import {
  sesGdList,
  addsesGd,
  editsesGd,
  detsesGd,
} from "@/api/sesGoodsMock.js";
export default {
  name: "sesGoodShip",
  data() {
    return {
      dialogtype: 1,
      ruleForm: {
        id:"",
        cat_id: "",
        name: "",
        nameId: "",
        price1: "",
        price2: "",
        remainNum: "",
        sesNum: "",
        num: "",
      },
      rules: {
        name: [{ required: true, message: "请输入活动名称", trigger: "blur" }],
      },
      dialogVisible: false,
      tableData: [],
      headerData: [
        {
          prop: "id",
          label: "编号",
        },
        {
          prop: "name",
          label: "活动标题",
        },
        {
          prop: "price1",
          label: "原价",
        },
        {
          prop: "price2",
          label: "秒杀价",
        },
        {
          prop: "sesNum",
          label: "秒杀数量",
        },
        {
          prop: "remainNum",
          label: "剩余数量",
        },
        {
          prop: "num",
          label: "限购数量",
        },
      ],
    
    };
  },
  mounted() {
    let _self = this;
    _self.userList();
  },
  methods: {
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
  
    submitForm(formName) {
      let _self = this;
      _self.dialogVisible = false;
      if (_self.dialogtype == 1) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let {cat_id,nameId,} =_self.$route.params
            let { name, price1, price2, remainNum, sesNum, num } =
              _self.ruleForm;
            let data = {
              cat_id,
              name,
              nameId,
              price1,
              price2,
              remainNum,
              sesNum,
              num,
            };
            addsesGd(data).then((res) => {
              console.log(res);
              if (res.meta.status == 200) {
                _self.$refs[formName].resetFields();
                console.log(_self.tableData);
                _self.userList();
              } else {
                _self.$message({
                  type: "info",
                  message: "res.meta.msg",
                });
              }
            });
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      } else {
        let _self = this;
        let {
        id,
        name,
        price2,
        sesNum,
        num,} = _self.ruleForm
        let data = {
        id,
        name,
        price2,
        sesNum,
        num,
        };
        
        editsesGd(data).then((res) => {
          console.log(res);
          if (res.meta.status == 200) {
            _self.userList();
            _self.ruleForm = {};
          }
        });
      }
    },
    userList() {
      let _self = this;
      console.log(_self.$route.params);
      let {cat_id,nameId,
     } =_self.$route.params
      let data = {
        cat_id,
        nameId,
      };
      sesGdList(data).then((res) => {
        console.log(res);
        if (res.meta.status == 200) {
          _self.tableData = [];
          _self.tableData = res.data.user;
          console.log(_self.tableData);
        }
      });
    },
    addfn() {
      let _self = this;
      _self.dialogtype=1
      _self.dialogVisible = true;
      _self.ruleForm = {};
    },
    goEdit(type, id, row) {
      console.log(row);
      let _self = this;
      _self.dialogtype = 2;
      _self.dialogVisible = true;
      _self.ruleForm = {
        id: row.id,
        name: row.name,
        price1: row.price1,
        price2: row.price2,
        remainNum: row.remainNum,
        sesNum: row.sesNum,
        num: row.num,
      };
    },
    godelete(id) {
      let _self = this;
      _self
        .$confirm("确定删除吗", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
        .then(() => {
          let data = { id };
          detsesGd(data).then((res) => {
            // console.log(res);
            if (res.meta.status == 200) {
              _self.$message({
                type: "success",
                message: "删除成功",
              });
              _self.userList();
            } else {
              _self.$message({
                type: "info",
                message: "res.meta.msg",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  components: { Suptable },
};
</script>
<style scoped>
.shuju {
  padding: 20px;
  margin: 20px 0;
  border: 1px solid #ebeef5;
  border-radius: 10px;
}
.right {
  float: right;
}
.clear {
  clear: both;
}
</style>